package Data;
import java.io.*;
import java.util.*;
public class SumSpecialOperator {
	private ArrayList<SumSpecialData> sumSpecialList = new ArrayList<SumSpecialData>();
	String filename = "D:\\SumSpecialRecord.txt";
	
	public SumSpecialOperator(){
		try{
			BufferedReader bf = new BufferedReader(new FileReader(filename));
			SumSpecialData ssd = null;
			String data = "";
			String[] record = null;
			while((data = bf.readLine()) != null){
				if(data.trim().equals("")){
					continue;
				}
				record = splitText(data);
				ssd = new SumSpecialData(Double.parseDouble(record[0]),Double.parseDouble(record[1]),record[2],record[3]);
				sumSpecialList.add(ssd);
				
			}
		}catch(IOException e){
			e.printStackTrace();
		}
	}
	
	public String[] splitText(String s){
		String[] data = s.split(",");
		return data;
	}
	
	
	public ArrayList<SumSpecialData> getSumSpecialData(){
		return sumSpecialList;
	}
	public void removeSumSpecial(double sumPrice,double discount,String sumSpecialStart,String sumSpecialEnd){
		String s = null;
		String s1 = "\r\n";
		try{
			BufferedWriter writer = new BufferedWriter(new FileWriter(new File(filename)));
			for(SumSpecialData ssd : sumSpecialList){
				if(ssd.getSumPrice() == sumPrice && ssd.getDiscount()==discount
						&& ssd.getSumSpecialStart().equals(sumSpecialStart) && ssd.getSumSpecialEnd().equals(sumSpecialEnd)){
					continue;
				}
				s = ssd.getSumPrice() + "," + ssd.getDiscount() + "," + ssd.getSumSpecialStart() + "," + ssd.getSumSpecialEnd();
				writer.write(s);
				writer.write(s1);
			}
						
			writer.close();
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public void savaSumSpecial(double sumPrice,double discount,String sumSpecialStart,String sumSpecialEnd){
		String s = null;
		String s1 = "\r\n";
		try{
			BufferedWriter writer = new BufferedWriter(new FileWriter(new File(filename)));
			sumSpecialList.add(new SumSpecialData(sumPrice, discount, sumSpecialStart, sumSpecialEnd));
			for(SumSpecialData ssd : sumSpecialList){
				s = ssd.getSumPrice() + "," + ssd.getDiscount() + "," + ssd.getSumSpecialStart() + "," + ssd.getSumSpecialEnd();
				writer.write(s);
				writer.write(s1);
			}
						
			writer.close();
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
}
